﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Drifting.Foundation;

namespace Drifting.Graphics.EffectObjects
{


    public class Line2D : EffectObject
    {
        static short[] quad_Indices;
        private VertexPositionColor[] vertices;
        private BasicEffect effects;
        private Matrix configuration;

        public Line2D(float width, Color color, GraphicsDevice device)
            : base(device)
        {
            vertices = new VertexPositionColor[4];

            Vector3 v0 = new Vector3(0, width,0);
            Vector3 v1 = new Vector3(0, -width,0);
            Vector3 v2 = new Vector3(1f,-width,0);
            Vector3 v3 = new Vector3(1f,width,0);

            vertices[0] = new VertexPositionColor(v0, color);
            vertices[1] = new VertexPositionColor(v1, color);
            vertices[2] = new VertexPositionColor(v2, color);
            vertices[3] = new VertexPositionColor(v3, color);


            if (quad_Indices == null)
            {
                quad_Indices = new short[] { 0, 1, 2, 
                                             0, 2, 3};

            }

            effects = new BasicEffect(device);
            configuration = Matrix.Identity;
        }

        public void Draw(Matrix view, Matrix projection, Vector3 v_start, Vector3 v_end)
        {
            Vector3 v_delta = v_end - v_start;
            float length = v_delta.Length();
            float sinT = v_delta.Y / length;
            float cosT = v_delta.X / length;
            float T = MathAssistant.AngleFromSinCos(sinT, cosT);


            Matrix config = Matrix.CreateScale(length, 1, 1) * 
                            Matrix.CreateRotationZ(T) * 
                            Matrix.CreateTranslation(v_start);

            effects.World = config;
            effects.VertexColorEnabled = true;
            effects.View = view;
            effects.Projection = projection;

            effects.CurrentTechnique.Passes[0].Apply();
            device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, 4, quad_Indices, 0, 2);
        }
        override public void Draw(Matrix view, Matrix projection)
        {
            effects.VertexColorEnabled = true;
            effects.World = configuration;
            effects.View = view;
            effects.Projection = projection;

            effects.CurrentTechnique.Passes[0].Apply();
            device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, 6, quad_Indices, 0, 2);

            throw new NotSupportedException("Call the overloaded Draw function instead");
        }


    }
}
